package com.spring.oaassist.web.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.google.common.collect.Maps;
import com.spring.common.web.service.CommonSelService;
import com.spring.oaassist.web.service.BudgetService;
import com.spring.security.core.annotation.OaUserPermission;

/**
 * 预算
 * @author wojunyu
 *
 */
@Controller
@RequestMapping(value = "/budget")
public class BudgetController {
	@Resource private BudgetService budgetService;
	@Resource private CommonSelService commonSelService;
	/**
	 * 预算报表主界面
	 * @param request
	 * @return
	 */
	@OaUserPermission(value="/budget/index")
	@RequestMapping(value = "/index")
    public ModelAndView index(HttpServletRequest request){
		Map<String,Object> retM = Maps.newHashMap();
		List<Map<String,Object>> lm =  commonSelService.getReportSel(null);
		retM.put("reportItem", lm);
		return new ModelAndView("main/budget/index",retM);
    }
	
	/**
	 * 预算报表列表界面,动态生成grid
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/content")
    public ModelAndView content(String reportID,String reportDate,String reportUnit,HttpServletRequest request){
		if(StringUtils.isEmpty(reportID)||StringUtils.isEmpty(reportDate)||StringUtils.isEmpty(reportUnit)){
			return new ModelAndView("404");
		}
		Map<String,Object> gridM= budgetService.getBudgetHeadList(reportID);
		gridM.put("reportID", reportID);
		gridM.put("reportDate", reportDate);
		gridM.put("reportUnit", reportUnit);
		return new ModelAndView("main/budget/content",gridM);
    }

	/**
	 * 查询报表数据
	 * @param budgetId
	 * @param budgetDate
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/queryData")
	@ResponseBody
    public Object queryData(String reportID,String reportDate,HttpServletRequest request){
		Map<String,Object> parm = Maps.newHashMap();
		parm.put("reportID", reportID);
		parm.put("reportDate", reportDate);
		List<Map<String,Object>> gridData= budgetService.queryBudgetData(parm);
		Map<String,Object> retD = Maps.newHashMap();
		retD.put("data", gridData);
		return retD;
    }
	
	@RequestMapping(value = "/saveBudget")
	@ResponseBody
    public Object saveBudget(@RequestBody List<Map<String,Object>> subjson,HttpServletRequest request){
		Map<String,Object> retM = budgetService.saveBudgetReport(subjson);
		return retM;
    }
}
